/**
 ** This file is part of FK Klistret Extensions. FK Klistret Extensions 
 ** is free software: you can redistribute it and/or modify it under the 
 ** terms of the GNU General Public License as published by the Free 
 ** Software Foundation, either version 3 of the License, or (at your option) 
 ** any later version.

 ** FK Klistret Extensions is distributed in the hope that it will be 
 ** useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 ** General Public License for more details. You should have received a
 ** copy of the GNU General Public License along with FK Klistret Extensions. 
 ** If not, see <http://www.gnu.org/licenses/>
 */
package se.fk.klistret.console.ui;

import java.util.Vector;
import java.util.Enumeration;

/**
 * 2011-07-11, The comments below are from 2005 and the code is has since
 * upgraded to the Ant 1.8 platform. The patterns and design of this project
 * should not be reused since they are not considered of any quality. The code
 * was written quickly without long term concerns.
 * <p>
 * 
 * 2005, TextMenuItem represents the basic information for every item defined
 * through TextMenuContainer containers. Also, delivery of events to subscribed
 * listeners is done through the fireEvent method.
 * 
 * @author Matthew Young
 */
public class TextMenuItem extends TextMenuComponent {
	/**
	 * (Not applicable)
	 */
	private String actionCommand;

	/**
	 * Vector array of subscribed listeners who will receive text menu events.
	 */
	private Vector<TextMenuEventListener> listeners;

	/**
	 * (Not applicable)
	 */
	private String text;

	/**
	 * Text label displayed
	 */
	private String label;

	/**
	 * 
	 */
	private Long id;

	/**
	 * 
	 */
	private Object entity;

	/**
	 * Confirmation string. Each TextMenuItem may have unique confirmations
	 * however control over whether the prompt is displayed (on/off) is managed
	 * by the TextMenu object.
	 */
	private String confirm_prompt;

	/**
	 * Returns display label
	 * 
	 * @return <code>String</code>
	 * 
	 */
	public String getLabel() {
		return this.label;
	}

	/**
	 * Sets display label/text
	 * 
	 */
	public void setLabel(String label) {
		this.label = label;
	}

	/**
	 * (Not applicable)
	 * 
	 */
	public String getText() {
		return this.text;
	}

	/**
	 * (Not applicable)
	 * 
	 */
	public void setText(String text) {
		this.text = text;
	}

	/**
	 * 
	 * @return
	 */
	public Long getId() {
		return this.id;
	}

	/**
	 * 
	 * @param id
	 */
	public void setId(Long id) {
		this.id = id;
	}

	/**
	 * Payload
	 * 
	 * @return
	 */
	public Object getEntity() {
		return this.entity;
	}

	/**
	 * 
	 * @param entity
	 */
	public void setEntity(Object entity) {
		this.entity = entity;
	}

	/**
	 * Sets the confirmation prompt.
	 * 
	 * @param prompt
	 *            <code>String</code>
	 * 
	 */
	public void setConfirmPrompt(String prompt) {
		this.confirm_prompt = prompt;
	}

	/**
	 * Return confirmation prompt
	 * 
	 */
	public String getConfirmPrompt() {
		return confirm_prompt;
	}

	/**
	 * Calls the fireEvent method
	 * 
	 * @param identifier
	 *            <code>int</code>
	 * 
	 */
	protected void processEvent(int identifier) {
		fireEvent(identifier, null);
	}

	/**
	 * Calls the fireEvent method
	 * 
	 * @param identifier
	 *            <code>int</code>
	 * @param command
	 *            <code>String</code>
	 * 
	 */
	protected void processEvent(int identifier, String command) {
		fireEvent(identifier, command);
	}

	/**
	 * (Not applicable)
	 * 
	 */
	public void setActionCommand(String actionCommand) {
		this.actionCommand = actionCommand;
	}

	/**
	 * (Not applicable)
	 * 
	 * 
	 */
	public String getActionCommand() {
		return this.actionCommand;
	}

	/**
	 * Adds a listener to the vector of subscribers.
	 * 
	 * @param listener
	 *            <code>TextMenuEventListener</code>
	 * 
	 */
	public synchronized void addActionListener(TextMenuEventListener listener) {
		if (listeners == null)
			listeners = new Vector<TextMenuEventListener>();

		listeners.addElement(listener);
	}

	/**
	 * Removes a listener to the vector of subscribers.
	 * 
	 * @param listener
	 *            <code>TextMenuEventListener</code>
	 * 
	 */
	public synchronized void removeActionListener(TextMenuEventListener listener) {
		if (listeners == null)
			listeners = new Vector<TextMenuEventListener>();

		listeners.removeElement(listener);
	}

	/**
	 * Fires events to all subscribed listeners also passing this object as the
	 * source or calling object.
	 * 
	 * @param identifier
	 *            <code>int</code>
	 * @param command
	 *            <code>String</code>
	 * 
	 */
	@SuppressWarnings("unchecked")
	protected synchronized void fireEvent(int identifier, String command) {
		if (listeners != null && !listeners.isEmpty()) {
			TextMenuEvent event = new TextMenuEvent(this, identifier, command);

			Vector<TextMenuEventListener> targets;

			synchronized (this) {
				targets = (Vector<TextMenuEventListener>) listeners.clone();
			}

			Enumeration<TextMenuEventListener> enumerator = targets.elements();
			while (enumerator.hasMoreElements()) {
				TextMenuEventListener listener = (TextMenuEventListener) enumerator
						.nextElement();

				listener.actionPerformed(event);
			}
		}
	}

	/**
	 * Equals
	 */
	public boolean equals(Object other) {
		if (this == other)
			return true;

		if (this.id == null)
			return false;

		if (!(other instanceof TextMenuItem))
			return false;

		final TextMenuItem that = (TextMenuItem) other;

		return this.id.equals(that.getId());
	}

	/**
	 * Constructs basic TextMenuItem with an empty label.
	 * 
	 */
	public TextMenuItem() {
		this("");
	}

	/**
	 * Constructs TextMenuItem setting the label attribute
	 * 
	 * @param label
	 *            <code>String</code>
	 * 
	 */
	public TextMenuItem(String label) {
		this.label = label;
	}

}
